import { _decorator, Component, Node } from 'cc';
import { SHARE_CHANNEL } from '../../ccc3-framework-core/const/MSG';
import dataUtil from '../../ccc3-framework-core/framework/modules/dataUtil';
import { entityUtil } from '../../ccc3-framework-core/framework/modules/entityUtil';
import { memoryConfig } from '../../ccc3-framework-core/framework/modules/memoryConfig';
import { uiManager } from '../../ccc3-framework-core/framework/modules/uiManager';
import { UiForm } from '../../ccc3-framework-core/framework/ui/uiForm';
import { gameState } from '../../ccc3-framework-core/gameState';
import { platformUtil } from '../../ccc3-framework-core/platform/platformUtil';
import Common from '../../ccc3-framework-core/utils/Common';
import { LeveLCfg } from '../../sheets/vo/LeveLCfg';
import { BundlesDefine, CategoryDefine, formDefine, pathDefine } from '../CustomDefine';
import { shareForm } from './shareForm';
const { ccclass, property } = _decorator;

@ccclass('endForm')
export class endForm extends UiForm {

    @property(Node)
    btnNext: Node = null;

    @property(Node)
    btnBack: Node = null;

    @property(Node)
    btnShare: Node = null;

    @property(Node)
    btnCancel: Node = null;

    start() {
        this.bindTouchEnd(this.btnNext, this.onNext);
        this.bindTouchEnd(this.btnBack, this.onBack);
        this.bindTouchEnd(this.btnCancel, this.onBack);
        this.bindTouchEnd(this.btnShare, this.onShare);
    }

    private onShare() {
        console.log("shareForm onShare");
        platformUtil.currentPlatform.share(SHARE_CHANNEL.VIDEO,
            (res) => {
                if (res) {
                    uiManager.showToast(`分享成功!`);
                }
            },
            (res) => {
                uiManager.showToast("分享失败")
            });
    }
    public onEnable() {
        //从游戏回来的
        platformUtil.currentPlatform.stopRecord((res) => {
            if (res == false) {
                console.log("不是抖音，不分享");
            }
            else if (res) {
                // uiManager.pushUIForm(pathDefine.formDir + formDefine.shareForm, (formNode) => {
                //     formNode.getComponent(shareForm).init();
                // }, BundlesDefine.main);
            }
        });

        // platformUtil.currentPlatform.showInter();
    }

    private onNext() {
        this.hideMap();
        uiManager.hideUIForm(formDefine.mapForm);
        uiManager.hideUIForm(formDefine.homeForm);
        uiManager.hideUIForm(formDefine.gameForm);
        uiManager.hideUIForm(formDefine.EndForm);
        memoryConfig.instance.level += 1;
        if (memoryConfig.instance.level > LeveLCfg.getAll().length - 1) {
            memoryConfig.instance.level = 0;
        }
        gameState.instance.nextGame(memoryConfig.instance.level);
    }

    private onBack() {
        this.hideMap();
        uiManager.hideUIForm(formDefine.gameForm);
        uiManager.hideUIForm(formDefine.EndForm);
        uiManager.pushUIForm(formDefine.homeForm);
    }

    private hideMap() {
        entityUtil.hideAllEntity(`level${Common.padLeft("" + (memoryConfig.instance.level + 1), 2, "0")}`, true);
    }



    update(deltaTime: number) {

    }
}

